<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        //深拷贝：序列化会丢失复杂数据
         let DadProto = {
            name:"张三",
            age:20,
            fn: function(){
                console.log("fn")
            },
            test: undefined,
            arr:[]
        }

        let SonProto = JSON.parse(JSON.stringify(DadProto))
        SonProto.name = "李四"
        console.log(DadProto)
        console.log(SonProto)

        //深拷贝
        function deepCopy(obj){
            let newObj = Array.isArray(obj) ? [] : {}
            for(let key in obj){
                if(obj.hasOwnProperty(key)){
                    if(typeof obj[key] === "object"){
                        newObj[key] = deepCopy(obj[key])
                    }else{
                        newObj[key] = obj[key]
                    }
                }
            }
            return newObj
        }
    </script>
</body>
</html>